//Lou Decroo
//updated 12-05-09

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;


public class CourseSurveyGUI extends JPanel implements ActionListener{

	private JPanel buttonRow, questionsGrid;
	private JButton submitSurveyButton, submitQAddButton;
	private QuestionPanel[] qPanels;
	private ArrayList<Response> userResp;
	private int numberOfSurveyQuestions;
	private JComboBox addQuestionBox;
	private int userStatus;
	private int currentCid;
	
	public CourseSurveyGUI(){
		this.setLayout(new BorderLayout());
		// remove this when integrating with system controller
		//setSurvey(null, 1);
	}

	public void actionPerformed(ActionEvent event) {
		if(event.getSource() == submitSurveyButton){
			GUIMaster gm = GUIMaster.getInstance();
			SystemController sc = GUIMaster.getWorkhorse();
			System.out.println("submitSurveyButton works");

			JOptionPane.showMessageDialog(null, "Thank you for completing this survey. \nYour input is greatly appreciated.");
			for(int i = 0; i<numberOfSurveyQuestions; i++){
				userResp.add(qPanels[i].getUserResponse());
			}
			Response [] r = new Response[userResp.size()];
			sc.submitResponses(currentCid, userResp.toArray(r));
			
			gm.surveySubmitted(); // move back to course selection page after survey is submitted 
									// will need to reload courses again to remove the taken survey from the menu
			System.out.println(userResp);
		}
		if(event.getSource() == submitQAddButton){
			System.out.println("submitQAddButton works");
			// get instance of guimaster
			GUIMaster gm = GUIMaster.getInstance();
			SystemController sc = gm.getWorkhorse();
			sc.currentCid = currentCid;
			
			if(addQuestionBox.getSelectedItem().equals("Add Multiple Choice Question")){
				// have guimaster set add mc question gui visible
				gm.mcQuestionAddRequested();
				System.out.println("moving to multiple choice question");
			}
			else if(addQuestionBox.getSelectedItem().equals("Add Open Ended Question")){
				// have guimaster set add oe question gui visible
				gm.oeQuestionAddRequested();
				System.out.println("moving to open ended question");
			}
		}

	}
	
    public void setSurvey(Survey s, int status, int cid)
    {
    	currentCid = cid;
		// set questions to questions array from database
    	Question[] questions;
		questions = new Question [s.getQuestions().size()];
		System.out.println(questions.length);
		questions = s.getQuestions().toArray(questions);
		userStatus = status;
		
		numberOfSurveyQuestions = questions.length;
		questionsGrid = new JPanel(new GridLayout(questions.length+1, 1));
		
		qPanels = new QuestionPanel[numberOfSurveyQuestions];//100]; // initialize size based on questions array from db
		userResp = new ArrayList <Response> ();
		
		String[] options = {"", "Add Multiple Choice Question", "Add Open Ended Question"};
		addQuestionBox = new JComboBox(options);
		addQuestionBox.addActionListener(this);
		

		submitSurveyButton = new JButton("SUBMIT");
		submitSurveyButton.addActionListener(this);
		
		submitQAddButton = new JButton("SUBMIT");
		submitQAddButton.addActionListener(this);

		buttonRow = new JPanel(new FlowLayout());
		if(userStatus == 2) // survey is not takable if status is 2(instructor) 
		{
			buttonRow.add(addQuestionBox);
			buttonRow.add(submitQAddButton);
			// set buttons inactive using button.setenabled(false)
			
			for(int i = 0; i<questions.length; i++){
				qPanels[i] = new QuestionPanel(questions[i], i+1, userStatus);
				questionsGrid.add(qPanels[i]);
				if(questions[i].getType() == 2){
					qPanels[i].disableJButtons();
				}
				else{
					qPanels[i].disableTextArea();
				}
			}
			
		}
		else{ // else user is a student
			buttonRow.add(submitSurveyButton);
			
			for(int i = 0; i<questions.length; i++){
				System.out.println(i + ") " + questions[i].getContent());
				qPanels[i] = new QuestionPanel(questions[i], i+1, userStatus);
				questionsGrid.add(qPanels[i]);
				if(questions[i].getType() == 2){
					qPanels[i].enableJButtons();
				}
				else{
					qPanels[i].enableTextArea();
				}
			}
		}
		

		questionsGrid.add(buttonRow);
		this.add(questionsGrid, BorderLayout.CENTER);
		

		this.setSize(500, 500);
		this.setVisible(true);

    }
	
	public static void main(String arg[])

	{
		JFrame f = new JFrame();
		JScrollPane scroll = new JScrollPane();
		
		scroll = new JScrollPane(new CourseSurveyGUI());
		scroll.getVerticalScrollBar();
		scroll.getHorizontalScrollBar();
		f.add(scroll);
		f.setSize(1000, 1000);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
